// pages/babymessage/babymessage.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    index: -1,
    kid: null,
    classid: null,
    classlist: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var list = wx.getStorageSync('classlist');
    var classlist = []
    // var classobj = {
    //   text: "全部",
    //   value: 0
    // }
    // classlist.push(classobj)
    for (var i = 0; i < list.length; i++) {
      var classobj = {
        text: list[i].className + "的档案管理",
        value: list[i].classID
      }
      classlist.push(classobj)
    }
    this.setData({
      classid: classlist[0].value,
      classlist: classlist
    })
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  getData: function () {
    var that = this
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "classid": that.data.classid,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "mobileType": app.globalData.mobileType,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_kidInfolist"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          that.setData({
            list: res.data.data.classList[0].kidList
          });


        } else {
          wx.showToast({
            title: "获取数据失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "获取数据失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },

  btnClick: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      index: index,
      kid: this.data.list[index]
    })
    console.log("index" + index)
    var kidStr = JSON.stringify(this.data.list[index])
    wx.navigateTo({
      url: '../babymessagedetail/babymessagedetail?kidStr=' + kidStr
    })
  },

  onclasschange({
    detail
  }) {
    this.setData({
      classid: detail
    });
    this.getData()
  },
})